package net.jiastudy.web.rest.controller.assessment;

import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.jiastudy.biz.assessment.entity.AssessDetailTreeVo;
import net.jiastudy.biz.assessment.entity.Assessment;
import net.jiastudy.biz.assessment.entity.AssessmentCreateVo;
import net.jiastudy.biz.assessment.entity.AssessmentDetail;
import net.jiastudy.biz.assessment.entity.AssessmentDetailShowVo;
import net.jiastudy.biz.assessment.entity.AssessmentPersonCreateVo;
import net.jiastudy.biz.assessment.entity.AssessmentScore;
import net.jiastudy.biz.assessment.service.AssessmentService;
import net.jiastudy.biz.user.entity.User;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/assessment")
public class AssessmentController {
    @Resource
    private AssessmentService assessmentService;

    @RequestMapping(method = RequestMethod.POST)
    @ApiOperation(value = "创建单个任务", notes = "")
    public void create(@RequestBody AssessmentCreateVo assessmentCreateVo) {
        assessmentService.addAssessment(assessmentCreateVo);
    }

    @RequestMapping(method = RequestMethod.GET)
    @ApiOperation(value = "检索列表", notes = "")
    public Page<Assessment> page(@RequestParam Map<String, Object> conditions,
                                 @PageableDefault(value = 50, sort = { "id" }, direction = Sort.Direction.DESC) Pageable pageable) {
        Page<Assessment> page = new Page<>(pageable.getPageNumber(), pageable.getPageSize());
        Sort sort = pageable.getSort();
        return assessmentService.searchAssessments(page, conditions, sort);
    }

    @RequestMapping(path = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "获取考核权重", notes = "")
    public List<AssessmentDetail> setWeight(@RequestParam Long assessmentId) {
        return assessmentService.getAndInitAssessmentDetails(assessmentId);
    }

    @RequestMapping(path = "/details", method = RequestMethod.PUT)
    @ApiOperation(value = "考核权重更新", notes = "")
    public boolean update(@RequestBody List<AssessmentDetail> details) {
        return assessmentService.updateAssessmentDetails(details);
    }

    @RequestMapping(path = "/tree", method = RequestMethod.GET)
    @ApiOperation(value = "打分树", notes = "")
    public AssessDetailTreeVo tree(@RequestParam Long yearId) {
        return assessmentService.getAssessmentDetailTree(yearId);
    }

    @RequestMapping(path = "/detail/{detailId}", method = RequestMethod.GET)
    @ApiOperation(value = "打分细节", notes = "")
    public AssessmentDetailShowVo getDetail(@PathVariable Long detailId) {
        return assessmentService.getAssessmentDetailShowVo(detailId);
    }

    @RequestMapping(path = "/scores", method = RequestMethod.POST)
    @ApiOperation(value = "打分", notes = "")
    public AssessmentScore setScore(@RequestBody AssessmentScore assessmentScore) {
        return assessmentService.createAssessmentScore(assessmentScore);
    }

    @RequestMapping(path = "/{id}/scores/users", method = RequestMethod.GET)
    @ApiOperation(value = "考核人列表", notes = "")
    public List<User> getScoreUsers(@PathVariable Long id) {
        return assessmentService.findAssessPersonsByAssessmentId(id);
    }

    @RequestMapping(path = "/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除")
    public Boolean delete(@PathVariable @ApiParam(name = "id", required = true) Long id) {
        return assessmentService.deletAssessment(id);
    }

    @RequestMapping(path = "/{id}/scores/users", method = RequestMethod.PUT)
    @ApiOperation(value = "更新考核人", notes = "")
    public boolean updateUser(@PathVariable Long id, @RequestBody AssessmentPersonCreateVo assessmentPersonCreateVo) {
        assessmentPersonCreateVo.setAssessmentId(id);
        assessmentService.updateAssessmentPerson(assessmentPersonCreateVo);

        return true;
    }
}
